import React, { Component } from "react";
// 这里引入story
import story from "../story";
export default class Home extends Component {
  constructor() {
    super();
    // 1.获取初始值
    this.state = story.getState();
    // 4.订阅
    this.unSubscribe = story.subscribe(() => {
      this.setState(story.getState());
    });
  }
  componentWillUnmount() {
    // 取消订阅===执行一下
    this.unSubscribe();
  }
  handleClick = () => {
    // 2.派发修改名称
    story.dispatch({
      type: "setName",
      value: "阿强"
    });
  };
  // 渲染
  render() {
    return (
      <div onClick={this.handleClick}>
        <h1 style={{ color: this.state.theme }}>
          {this.state.name} 首页欢迎您
        </h1>
      </div>
    );
  }
}
